﻿using System;
using System.Collections.Generic;
using System.Text;
using TVOnline;
using TVOnline.Entities;
using EricUtility;
using System.Net;

namespace WatchMoviesHelper.Entities
{
    public class MovieInfo : AbstractLoadingTuple
    {
        private string m_Id;
        private string m_Name;
        private string m_Description = "";

        public string Name { get { return m_Name; } }
        public string Id { get { return m_Id; } }
        public string Description { get { return m_Description; } }
        private readonly Dictionary<string, WebsiteMovieInfo> m_WebSites = new Dictionary<string, WebsiteMovieInfo>();
        private readonly Dictionary<KnownWebsite, WebsiteMovieInfo> m_KnownWebSites = new Dictionary<KnownWebsite, WebsiteMovieInfo>();
        public WebsiteMovieInfo[] WebSites
        {
            get
            {
                WebsiteMovieInfo[] array = new WebsiteMovieInfo[m_WebSites.Count];
                m_WebSites.Values.CopyTo(array, 0);
                return array;
            }
        }

        public MovieInfo(string id, string name)
        {
            m_Id = id;
            m_Name = name.Trim();
        }

        protected override void OnLoad()
        {
            m_WebSites.Clear();
            m_KnownWebSites.Clear();

            CookieContainer cookies = new CookieContainer();
            string s = EricUtility.Networking.Gathering.GatheringUtility.GetPageSource("http://watch-movies-az.com/movie/" + m_Id, cookies);
            string descDiv = "<div class=\"episode-summary\">";
            int iDescDiv = s.IndexOf(descDiv);
            string descTd = "<span class=\"no-summary-vid\">";
            int iDescTd = s.IndexOf(descTd, iDescDiv) + descTd.Length;
            int iDescTdEnd = s.IndexOf("</span>", iDescTd);
            m_Description = WebStringUtility.DecodeString(s.Substring(iDescTd, iDescTdEnd - iDescTd)).Trim();



            string deb = "<div class=\"linewrap\">";
            while (s.IndexOf(deb) >= 0)
            {
                s = s.Substring(s.IndexOf(deb) + deb.Length);
                string siteDeb = "<div class=\"site\">";
                string divFin = "</div>";
                int posSiteDeb = s.IndexOf(siteDeb) + siteDeb.Length;
                int posSiteFin = s.IndexOf(divFin, posSiteDeb);
                string site = s.Substring(posSiteDeb, posSiteFin - posSiteDeb).Trim();
                int posDlDeb = s.IndexOf("<div class=\"download\">", posSiteFin);
                int posUrlFin = 0;
                int posUrlDeb = 0;
                string urlDeb = "<a href=\"../open/cale/";
                string urlFin = "/idpart/";
                List<string> urls = new List<string>();
                while ((posUrlDeb = s.IndexOf(urlDeb, posUrlFin) + urlDeb.Length) >= 0 && posUrlDeb > posUrlFin && posUrlDeb < posDlDeb)
                {
                    posUrlFin = s.IndexOf(urlFin, posUrlDeb);
                    string url = s.Substring(posUrlDeb, posUrlFin - posUrlDeb);
                    urls.Add(url);
                }
                AddLink(site, urls,cookies);
            }
        }
        public WebsiteInfo KnownWebSite(KnownWebsite t)
        {
            if (!m_KnownWebSites.ContainsKey(t))
                return null;
            return m_KnownWebSites[t];
        }
        private void AddLink(string site, List<string> urls, CookieContainer cookies)
        {
            WebsiteMovieInfo w;
            if (m_WebSites.ContainsKey(site))
                w = m_WebSites[site];
            else
            {
                w = new WebsiteMovieInfo(this, site);
                if (m_WebSites.ContainsKey(w.Name))
                {
                    w = m_WebSites[w.Name];
                }
                else
                {
                    if (w.TypeWebSite != KnownWebsite.Others)
                        m_KnownWebSites.Add(w.TypeWebSite, w);
                    m_WebSites.Add(w.Name, w);
                }
            }
            LinkMovieInfo l = new LinkMovieInfo(w, w.Mirrors.Length + 1);
            int count = 1;
            foreach (string s in urls)
                l.AddLink(new LinkPartMovieInfo(l, s, count++,cookies));
            w.AddMirror(l);
        }
        public override string ToString()
        {
            return m_Name;
        }
    }
}
